package org.ufinity.hsms.module.setting;

import org.ufinity.hsms.R;
import org.ufinity.hsms.common.activity.BaseActivity;
import org.ufinity.hsms.common.application.HSMApplication;
import org.ufinity.hsms.common.utils.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class LanguageSettingActivity extends BaseActivity implements OnClickListener{
	public static final String TAG = "SettingActivity";
	private HSMApplication application;
	
	RadioGroup radioGroup;
	RadioButton radio1;
	RadioButton radio2;
	RadioButton radio3;
	RadioButton radio4;
	RadioButton radio5;
	RadioButton radio6;
	
	Button btn_save;
	String language = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		application = getBaseApplication();
		application.addActivity(instance);
		setContentView(R.layout.setting_language);
		initView();
		initDate();
	}

	private void initDate() {
		SharedPreferences sharedPreferences = this.getSharedPreferences("language_settings", Activity.MODE_PRIVATE);
		if(!StringUtils.isBlank(sharedPreferences.getString("language", ""))){
			language = sharedPreferences.getString("language", "");
			if(language.equals("ch_t")){
				radioGroup.check(radio2.getId());
			}else if(language.equals("en")){
				radioGroup.check(radio3.getId());
			}else if(language.equals("german")){
				radioGroup.check(radio4.getId());
			}else if(language.equals("russian")){
				radioGroup.check(radio5.getId());
			}else if(language.equals("french")){
				radioGroup.check(radio6.getId());
			}else{
				radioGroup.check(radio1.getId());
			}
		}
	}

	private void initView() {
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				if(checkedId == radio1.getId()){
					//中文
					language = "ch";
				}else if(checkedId == radio2.getId()){
					//繁体中文
					language = "ch_t";
				}else if(checkedId == radio3.getId()){
					//英文
					language = "en";
				}else if(checkedId == radio4.getId()){
					//德文
					language = "german";
				}else if(checkedId == radio5.getId()){
					//俄文
					language = "russian";
				}else if(checkedId == radio6.getId()){
					//法文
					language = "french";
				}
			}
		});
		
		radio1 = (RadioButton) findViewById(R.id.radio1);
		radio1.setOnClickListener(this);
		radio2 = (RadioButton) findViewById(R.id.radio2);
		radio2.setOnClickListener(this);
		radio3 = (RadioButton) findViewById(R.id.radio3);
		radio3.setOnClickListener(this);
		radio4 = (RadioButton) findViewById(R.id.radio4);
		radio4.setOnClickListener(this);
		radio5 = (RadioButton) findViewById(R.id.radio5);
		radio5.setOnClickListener(this);
		radio6 = (RadioButton) findViewById(R.id.radio5);
		radio6.setOnClickListener(this);
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_save.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == btn_save.getId()){
			save();
		}
	}

	private void save() {
		
		SharedPreferences sharedPreference = this.getSharedPreferences("language_settings", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreference.edit();
		editor.putString("language", language);
		editor.commit();
		makeMyToast(instance, getResources().getString(R.string.success));
	}
}
